/**
 * Created by LeungZ on 2016/9/7.
 */
import jsYaml from 'js-yaml';
class Ctrl {
    constructor($modalInstance, service, version, appSrv, ctgNotification, updateVer) {
        "ngInject";
        this._modalInstance = $modalInstance;
        this._notification = ctgNotification;
        this._appSrv = appSrv;
        this._updateVer = updateVer;

        this.advCtrl = {
            podDomain: service.url,
            atsCheck: !!version.isAutoElastic,
            atsTarget: version.elasticIndicator,
            maxPodNum: version.maxPodNum,
            minPodNum: version.minPodNum,
            atsStartTime: new Date(version.elasticStartTime).getTime(),
            atsEndTime: new Date(version.elasticEndTime).getTime(),
            atsSpace: version.timeRate,
            atsLimit: version.maxCpu
        };

        this.serviceId = service.serviceId;
        this.versionId = version.versionId;
    }

    save() {
        this.advCtrl[this.advCtrl.currentType + 'Form'].$submitted = true;
        if (this.advCtrl[this.advCtrl.currentType + 'Form'].$invalid) return;

        this.loading = true;
        this._appSrv.advUpdate(this.advCtrl, this.serviceId, this.versionId).then(()=> {
            this._notification.success('更新成功.');
            this._appSrv.getVerDetail(this.versionId).then(data=>{
                this._updateVer(data);
            }).finally(()=>{
                this.loading = false;
            });
        }).catch(data=> {
            this.loading = false;
            this._notification.error('更新失败, ' + data.resultMsg);
        });
    }

    cancel() {
        this._modalInstance.dismiss('cancel');
    }

}
export default Ctrl